﻿using ProjectCloth.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI.Companys
{
    public partial class EditCompanyFrm : Form
    {
        private ClothContext db = new ClothContext();
        private string _companyName = "";
        private int _companyId = 0;
        private bool isNew = true;
        public EditCompanyFrm()
        {
            InitializeComponent();
            this.Text = "添加供应商";
        }
        public EditCompanyFrm(string companyNo)
        {
            InitializeComponent();
        }
        public EditCompanyFrm(int companyId)
        {
            InitializeComponent();
            isNew = false;
            var company = db.Companies.Find(companyId);
            if (company != null)
            {
                _companyName = company.Name;
                _companyId = companyId;
                companyNameTxt.Text = company.Name;
                remarkTxt.Text = company.Remark;
            }
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (isNew)
            {
                if (string.IsNullOrEmpty(companyNameTxt.Text))
                {
                    MessageBox.Show("名称不能为空！");
                    return;
                }
                var company = new Company()
                {
                    Name = GetCompanyName(companyNameTxt.Text),
                    Remark = remarkTxt.Text
                };
                var count = db.Companies.Where(o => o.Name == companyNameTxt.Text).Count();
                if (count == 0)
                {
                    db.Companies.Add(company);
                    if (db.SaveChanges()>0)
                    {
                        MessageBox.Show("添加成功！");
                    }

                }
                else
                {
                    MessageBox.Show("添加失败，发现重复名称！");
                }

            }
            else
            {
                if (_companyId > 0)
                {
                    if (string.IsNullOrEmpty(companyNameTxt.Text))
                    {
                        MessageBox.Show("名称不能为空！");
                        return;
                    }
                    var company=  db.Companies.Find(_companyId);
                    if (company != null)
                    {
                        company.Name = companyNameTxt.Text;
                        company.Remark = remarkTxt.Text;
                        if (db.SaveChanges() > 0)
                        {
                            MessageBox.Show("保存成功！");
                        }
                    }
                }
            }
        }
        private string GetCompanyName(string name)
        {
            var companyCount = db.Companies.Count()+1;
            var companyName = (companyCount).ToString() + name;
           var count= db.Companies.Where(o => o.Name== companyName).Count();
            while (count > 0)
            {
                companyCount++;
                companyName = (companyCount).ToString() + name;
                count = db.Companies.Where(o => o.Name == companyName).Count();
            }

            return companyName;
        }
    }
}
